﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ecdl.demo.model.expression {
    public abstract class Expression {

        public virtual ExpressionState Evaluate() {
            return Evaluate(ExpressionContext.Empty);
        }

        public abstract ExpressionState Evaluate(ExpressionContext context);

        protected ExpressionState Fail(string posCause, string negCause) {
            LeafExpressionState state = new LeafExpressionState(this);
            state.IsOk = false;
            state.SetCause(posCause, negCause);
            return state;
        }

        protected ExpressionState Fail(string neutralCause) {
            return Fail(neutralCause, neutralCause);
        }

        protected ExpressionState Fail(Exception e) {
            return new ErrorexpressionState(this, e);
        }

        protected ExpressionState Success(string posCause, string negCause) {
            LeafExpressionState state = new LeafExpressionState(this);
            state.IsOk = true;
            state.SetCause(posCause, negCause);
            return state;
        }
    }
}
